package com.vertigrated.db.mssql;

import com.vertigrated.temporal.DateUtil;
import com.vertigrated.temporal.Date;
import org.testng.annotations.Test;

/**
 * Test the Microsoft SQL Server SelectBuilder.
 * In real use SelectBuilder can only be obtained from a Database instance.
 */
public class TestSelectBuilder
{
    @Test
    public void testStringIsNull()
    {
        final SelectBuilder sb = new SelectBuilder("tablename");
        sb.addColumnName("col1", "SOME_DATA");
        assert "SELECT ISNULL(col1,'SOME_DATA') FROM tablename".equals(sb.getCommand()) : sb.getCommand();
    }

    @Test
    public void testNumberIsNull()
    {
        final SelectBuilder sb = new SelectBuilder("tablename");
        sb.addColumnName("col1", Integer.MAX_VALUE);
        assert "SELECT ISNULL(col1,2147483647) FROM tablename".equals(sb.getCommand()) : sb.getCommand();
    }

    @Test
    public void testDateIsNull()
    {
        final SelectBuilder sb = new SelectBuilder("tablename");
        sb.addColumnName("col1", new Date(DateUtil.BEGINNING_OF_TIME));
        assert "SELECT ISNULL(col1,'0001-12-31 19:00:00.000') FROM tablename".equals(sb.getCommand()) : sb.getCommand();
    }

    @Test
    public void testStringWithAliasIsNull()
    {
        final SelectBuilder sb = new SelectBuilder("tablename");
        sb.addColumnName("col1", "c1", "SOME_DATA");
        assert "SELECT ISNULL(col1,'SOME_DATA') as c1 FROM tablename".equals(sb.getCommand()) : sb.getCommand();
    }

    @Test
    public void testNumberWithAliasIsNull()
    {
        final SelectBuilder sb = new SelectBuilder("tablename");
        sb.addColumnName("col1", "c1", Integer.MAX_VALUE);
        assert "SELECT ISNULL(col1,2147483647) as c1 FROM tablename".equals(sb.getCommand()) : sb.getCommand();
    }

    @Test
    public void testDateWithAliasIsNull()
    {
        final SelectBuilder sb = new SelectBuilder("tablename");
        sb.addColumnName("col1", "c1", new Date(DateUtil.BEGINNING_OF_TIME));
        assert "SELECT ISNULL(col1,'0001-12-31') as c1 FROM tablename".equals(sb.getCommand()) : sb.getCommand();
    }
}
